package com.movieknight;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.github.savvasdalkitsis.jtmdb.Movie;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

// Movies Tab. Displays a list of current movies, and contains a menu

public class RecommendListActivity extends Activity {	
	private static final String LOG_TAG = TMDBMovieDownloader.class.getSimpleName();
    public void onCreate(Bundle savedInstanceState) {
    	Log.v(LOG_TAG, "Test1");
		super.onCreate(savedInstanceState);
		setTitle("Recommendations:");

    	Intent theIntent = getIntent();
    	ArrayList<String> ranks = new ArrayList<String>();
    	ranks = theIntent.getStringArrayListExtra("ranks");
    	ArrayList<String> IMDBIDs = theIntent.getStringArrayListExtra("IDs");
    	List<Movie> movies = new ArrayList<Movie>();

    	for(int i = 0; i < IMDBIDs.size(); i++) {
			Log.v(LOG_TAG, "z: " + IMDBIDs.get(i));
			List<Movie> tempList;
			Log.v(LOG_TAG, "x: " + IMDBIDs.get(i));
			try {
				Log.v(LOG_TAG, "a: " + IMDBIDs.get(i));
				tempList = Movie.imdbLookup(IMDBIDs.get(i).toString());
				Log.v(LOG_TAG, "b: " + IMDBIDs.get(i));
				if(tempList.size()>0)
				{
					Movie temp = tempList.get(0);

					Log.v(LOG_TAG, "c: " + IMDBIDs.get(i));
					movies.add(temp);
				}
				Log.v(LOG_TAG, "d: " + IMDBIDs.get(i));
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}
    	}
    	
		ListView listView = new ListView(getBaseContext());
		listView.setAdapter(new MovieAdapter(getBaseContext(), movies, (MovieKnightApp) getApplication()));
		this.setContentView(listView);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) {
				Intent intent = new Intent(getBaseContext(), AddRecommendationInfoPageActivity.class);
				intent.putExtra("movieID", (int) id);
				startActivityForResult(intent, 0);
			}
		});
    }
    
	@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
        		Intent data2 = new Intent();
        		setResult(RESULT_OK, data2);
        		finish();
            }
        }
    }
}